var api = require('../../utils/api.js');
var date = require('../../utils/date.js');

var app = getApp();
Page({
    data: {
        user:[],
        imgs:'',
        work:'',
        daka:'上班打卡',
        nowaddress:''
    },
    currentPageNumber: 1,
    onLoad() {
        let user = wx.getStorageSync('userinfo');
        if (!user) {
            wx.redirectTo({
                url: '/pages/login/login'
            });
        }
        this.getmyaddress();
    },

    getmyaddress(){
        var that=this;
        wx.getLocation({
            type: 'gcj02', 
            success: function(res) {
              console.log('定位成功', res.latitude, res.longitude);


              var location=res.latitude+','+res.longitude;
          
              api.get({
                  url: 'index/getaddname',
                  data: {location:location},
                  success: data => {
                      that.setData({
                          nowaddress:data.data
                      })
                  }
              });
        
            },
            fail: function(err) {
              console.error('定位失败', err);

            }
          });
    },


    onShow() {
        
        var that=this;
        api.get({
            url: 'user/index',
            data: {},
            success: data => {
                this.setData({
                    user:data.data,
                    imgs:data.data.avatar
                })
            }
        });

        setInterval(function(){
            var time=  (new Date()).format("h:m:s") ;
            that.setData({
                time:time
            })
        },1000)

        this.gettodylog();

    },
   
    gettodylog(){
        var that=this;

        api.get({
            url: 'work/gettodylog',
            data: {},
            success: data => {
                that.setData({
                    work:data.data,
                    daka:'上班打卡'
                })

                if(data.data.startime){
                    that.setData({
                        daka:'下班打卡'
                    })
                }

            }
        });
    },

    newdaka(){
        var that=this;

        api.get({
            url: 'work/submits',
            data: {},
            success: data => {
                
                console.log(data);
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })

                that.gettodylog();

            }
        });


    },

    searchSubmit() {
        this.setData({
            searchingResult: true,
            searchKeyword: this.data.searchInputVal
        });
        this.onPullDownRefresh();
    },
    previewImage(e) {
        wx.previewImage({
            current: '', // 当前显示图片的http链接
            urls: [e.currentTarget.dataset.previewUrl] // 需要预览的图片http链接列表
        });

    },
    onShareAppMessage() {
        // return {
        //     title: '好玩的,都在这儿~~',
        //     path: '/pages/index/index'
        // }
    },

    switchover(){
        wx.navigateTo({
          url: '/pages/index/show'
        })
    }

});
